<template>
  <el-container style="height: 100vh; border: 1px solid #eee;">
    <!-- 左侧导航栏 -->
    <el-aside width="200px" style="background-color: #2b3340;">
      <div class="logo">
        <img
            src="@/assets/logo.png"
            alt="logo"
            style="width: 50px; height: 50px; margin-right: 10px;"
            onerror="this.src='https://via.placeholder.com/50'; this.onerror=null;"
        >
        <span style="color: #fff; font-size: 18px;">用户管理系统</span>
      </div>
      <el-menu
          default-active="1"
          class="el-menu-vertical"
          background-color="#2b3340"
          text-color="#fff"
          active-text-color="#409EFF">
        <el-submenu index="1">
          <template slot="title">
            <i class="el-icon-menu"></i>
            <span>系统管理</span>
          </template>
          <el-menu-item index="1-1" @click="goToPage('/zhuye/user')">用户管理</el-menu-item>
          <el-menu-item index="1-2" @click="goToPage('/zhuye/order')">商品管理</el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <!-- 右侧主体区域 -->
    <el-container>
      <!-- 顶部栏 -->
      <el-header style="text-align: right; font-size: 12px; display: flex; justify-content: flex-end; align-items: center; background-color: #fff;">
        <div style="margin-right: 20px;">当前用户: {{ username }}</div>
        <el-button type="primary" plain @click="logout">退出登录</el-button>
      </el-header>
      <!-- 主内容区域，用于渲染子路由 -->
      <el-main style="background-color: #f0f2f5;">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Zhuye",
  data() {
    return {
      username: ''
    };
  },
  mounted() {
    const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
    this.username = userInfo.username || '未登录';
  },
  methods: {
    logout() {
      localStorage.removeItem('userInfo');
      this.$message.success("退出登录成功");
      this.$router.push("/");
    },
    goToPage(targetPath) {
      // 获取当前路径（this.$route.path 是当前路由的完整路径）
      const currentPath = this.$route.path;
      // 只有当前路径与目标路径不一致时，才执行跳转
      if (currentPath !== targetPath) {
        this.$router.push(targetPath);
      }
    },
  }
};
</script>

<style scoped>
.logo {
  display: flex;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #424c5b;
}
.el-menu-vertical {
  border-right: none;
}
</style>